package sivan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectDemo {

    public AspectDemo() {
        System.out.println("=======================");
    }

    /**
     * 前置通知 可以获取切入点信息
     *
     * @param joinPoint
     */
    @Pointcut("@annotation(sivan.aspect.RateLimit)")
    private void lockPoint() {
    }

    @Before("lockPoint()")
    public void test() {
        System.out.println("测试aop");
    }

    public void chekPri(JoinPoint joinPoint) {
        System.out.println("权限校验" + joinPoint);
    }

    /**
     * 后置通知 可以获取返回值内容
     *
     * @param result
     */
    @After(value="lockPoint()")
    public void wlog() {
        System.out.println("日志记录。。。" );
    }

    /**
     * 环绕通知  -性能监控
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
//    @Around("lockPoint()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object object = joinPoint.proceed(); //执行目标程序
        System.out.println("环绕后通知");
        System.out.println("总共执行了3s");
        return object;
    }

    /**
     * 异常抛出
     */
    @AfterThrowing(value="lockPoint()",throwing = "ex")
    public void afterThrow(Throwable ex) {
        System.out.println("异常抛出.." + ex.getMessage());
    }

    /**
     * 最终通知，相当于 finally
     */
    @AfterReturning("lockPoint()")
    public void after() {
        System.out.println("最终通知...");
    }
}
